package proto_wearebatman;

public abstract class Gate implements IhasInput, IhasOutput {

    protected Input[] inputs;
    protected Output[] outputs;
    protected Wire[] wireInputs;
    protected Wire[] wireOutputs;
    protected String name;
    public int counter;
    protected int prevInput0, prevInput1;

    public Gate(String name, int in, int out) {
        this.outputs = new Output[out];
        for (int i = 0; i < out; i++) {
            this.outputs[i] = new Output();
        }
        this.inputs = new Input[in];
        for (int i = 0; i < in; i++) {
            this.inputs[i] = new Input();
        }
        this.wireOutputs = new Wire[out];
        this.wireInputs = new Wire[in];
        this.name = name;
        this.counter = 0;
        this.prevInput0 = 0;
        this.prevInput1 = 0;
    }

    public abstract void operate();

    public void setInputData(int pin, int data) {
        this.inputs[pin].setData(data);
    }

    public int getInputData(int pin) {
        return this.inputs[pin].getData();
    }

    public int getOutputData(int pin) {
        return this.outputs[pin].getData();
    }

    public void setOutputData(int pin, int data) {
        this.outputs[pin].setData(data);
    }

    public void connectWireInput(int pin, Wire wire) {
        this.wireInputs[pin] = wire;
    }

    public void connectWireOutput(int pin, Wire wire) {
        this.wireOutputs[pin] = wire;
    }

    public String getName() {
        return this.name;
    }

    public String getFinalState() {
        return Integer.toString(this.outputs[0].getData());
    }
}
